//
//  CommitViewController.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/9/12.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "CommitViewController.h"
#import "LHRatingView.h"
@interface CommitViewController ()<UITextViewDelegate, UIGestureRecognizerDelegate, ratingViewDelegate>
@property (nonatomic, strong) UILabel *placeLabel;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *aview;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, copy) NSString *imageUrl;
@property (nonatomic, strong) ImageActionSheet *imageActionSheet;
@property (nonatomic, copy) NSString *select;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, assign) NSInteger score;
@end

@implementation CommitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackItem];
    self.title = @"评价表白";
    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(handleBtn:)];
    btn.tintColor = ColorFrom16(0xEDC330);
    NSDictionary *dic = @{NSFontAttributeName:PFRFONT(18)};
    [btn setTitleTextAttributes:dic forState:0];
    self.navigationItem.rightBarButtonItem = btn;
    _select = @"0";
    _score = 1;
    [self initView];
}

- (void)startNetwork
{
    if([GW_Tool isEmptyString:_textView.text])
    {
        MESSAGE_TOAST(@"请输入评价内容~");
        return;
    }
    NSDictionary *param = @{@"orderSn":STRING(self.orderString),
                            @"content":STRING(_textView.text),
                            @"niming" : _select,
                            @"num"    : @(_score),
                            @"images" : STRING(_imageUrl)};
    [NetWork sendPostRequestURL:[NSString stringWithFormat:@"%@/order/setOrderCommon", GW_Domain] parameters:param completionBlockWithSuccess:^(NSDictionary *resultDict) {
        MESSAGE_TOAST(resultDict[@"message"]);
        _bgView.hidden = NO;
    } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"%@", error);
    }];
}


//提交响应方法
- (void)handleBtn:(UIBarButtonItem *)sender
{   [self.view endEditing:YES];
    [self startNetwork];
}

- (void)initView
{
    UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 60, 60)];
    iv.image = IMAGE(@"mine_commit_img");
    [self.view addSubview:iv];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(90, 16 , 30.2, 20)];
    label.text = @"评分";
    label.textColor = ColorFrom16(0x323232);
    label.font = PFRFONT(14);
    [self.view addSubview:label];
    
    LHRatingView * rView = [[LHRatingView alloc]initWithFrame:CGRectMake(99, 44, 150, 30)];
    //    rView.ratingType = INTEGER_TYPE;//整颗星
    rView.delegate = self;
    [self.view addSubview:rView];
    
    _textView = [UITextView new];
    _textView.layer.backgroundColor = ColorFrom16(0xf5f5f5).CGColor;
    _textView.delegate = self;
    _textView.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.view addSubview:_textView];
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iv.mas_bottom).offset(10);
        make.left.and.right.equalTo(self.view);
        make.height.equalTo(@123);
    }];
    
    
    _placeLabel = [UILabel new];
    _placeLabel.text = @"说点什么~";
    _placeLabel.textColor = ColorFrom16(0x969696);
    _placeLabel.font = PFRFONT(12);
    [_textView addSubview:_placeLabel];
    [_placeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_textView).offset(7);
        make.left.equalTo(_textView).offset(10);
        make.width.greaterThanOrEqualTo(@60);
        make.height.equalTo(@40);
    }];
    
    UILabel *alabel = [[UILabel alloc] init];
    alabel.text = @"最多100字";
    alabel.textColor = ColorFrom16(0x969696);
    alabel.font = PFRFONT(10);
    [_textView addSubview:alabel];
    [alabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(180);
        make.right.equalTo(self.view).offset(-10);
        make.width.equalTo(@48);
        make.height.equalTo(@14);
    }];
    
    UIButton *photoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [photoBtn setImage:IMAGE(@"mine_photo") forState:0];
    [photoBtn addTarget:self action:@selector(changeHeadImage) forControlEvents:UIControlEventTouchUpInside];
    photoBtn.layer.borderWidth = 1;
    photoBtn.layer.borderColor = ColorFrom16(0xe2e2e2).CGColor;
    [self.view addSubview:photoBtn];
    [photoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_textView.mas_bottom).offset(21);
        make.left.equalTo(self.view).offset(16);
        make.width.equalTo(@77);
        make.height.equalTo(@77);
    }];
    
    //匿名
    UIButton *selectImg = [UIButton buttonWithType:UIButtonTypeCustom];
    [selectImg setImage:IMAGE(@"mine_selectImg") forState:0];
    selectImg.layer.borderColor = ColorFrom16(0xe1e1e1).CGColor;
    selectImg.layer.borderWidth = 1;
    [self.view addSubview:selectImg];
    [selectImg addTarget:self action:@selector(selectBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [selectImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(photoBtn.mas_bottom).offset(17);
        make.left.equalTo(self.view).offset(16);
        make.width.equalTo(@18);
        make.height.equalTo(@18);
    }];
    
    UILabel *reminderLabel = [UILabel new];
    reminderLabel.text = @"匿名评价";
    reminderLabel.textColor = ColorFrom16(0x323232);
    reminderLabel.font = PFRFONT(12);
    [self.view addSubview:reminderLabel];
    [reminderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(photoBtn.mas_bottom).offset(18);
        make.left.equalTo(selectImg.mas_right).offset(12);
        make.width.equalTo(@50);
        make.height.equalTo(@17);
    }];
    
    [self createReminderView];
}


- (void)selectBtnAction:(UIButton *)sender
{
    _select = @"1";
    [sender setImage:IMAGE(@"") forState:0];
    [sender addTarget:self action:@selector(handleSender:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)handleSender:(UIButton *)sender
{
    _select = @"0";
    [sender setImage:IMAGE(@"mine_selectImg") forState:0];
    [sender addTarget:self action:@selector(selectBtnAction:) forControlEvents:UIControlEventTouchUpInside];

}

//分享弹出框
- (void)createReminderView
{
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GWIDTH, GHEIGHT)];
    _bgView.backgroundColor = ColorFrom16A(0x000000, 0.8);
    _bgView.hidden = YES;
    [self.navigationController.view addSubview:_bgView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAction)];
    tap.delegate = self;
    [_bgView addGestureRecognizer:tap];
    
    _aview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 180)];
    _aview.layer.backgroundColor = ColorWhite.CGColor;
    _aview.layer.cornerRadius = 12;
    _aview.center = _bgView.center;
    [_bgView addSubview:_aview];
    
    UIImageView *iv = [UIImageView new];
    iv.image = IMAGE(@"correct");
    iv.layer.backgroundColor = ColorClear.CGColor;
    iv.layer.cornerRadius = 67 / 2;
    [_aview addSubview:iv];
    [iv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_aview).offset(-29);
        make.centerX.equalTo(_aview.mas_centerX);
        make.width.equalTo(@67);
        make.height.equalTo(@67);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"评价成功";
    titleLabel.textColor = ColorFrom16(0xF5CB37);
    titleLabel.font = PFRFONT(17);
    titleLabel.textAlignment = 1;
    [_aview addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iv.mas_bottom).offset(24);
        make.centerX.equalTo(_aview.mas_centerX);
        make.width.greaterThanOrEqualTo(@100);
        make.height.equalTo(@24);
    }];
    
    UILabel *label = [UILabel new];
    label.text = @"感谢您的用心评价哦~么么哒！";
    label.textColor = ColorFrom16(0xA7A7A7);
    label.numberOfLines = 2;
    label.adjustsFontSizeToFitWidth = YES;
    label.textAlignment = 1;
    label.font = PFRFONT(13);
    [_aview addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(10);
        make.width.equalTo(@240);
        make.height.equalTo(@32);
        make.centerX.equalTo(_aview.mas_centerX);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"立即查看" forState:0];
    [button setTitleColor:ColorFrom16(0x323232) forState:0];
    button.titleLabel.font = PFRFONT(17);
    button.backgroundColor = ColorFrom16(0xFEDE6D);
    [_aview addSubview:button];
    button.frame = CGRectMake(0, 140, 270, 40);
    [button addTarget:self action:@selector(shareBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    //切圆角  任意切两个
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:button.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
    maskLayer.frame = button.bounds;
    maskLayer.path = maskPath.CGPath;
    button.layer.mask = maskLayer;
    
    _bottomView = [UIView new];
    _bottomView.hidden = YES;
    _bottomView.backgroundColor = ColorFrom16(0xffffff);
    [_bgView addSubview:_bottomView];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_bgView);
        make.left.and.right.equalTo(_bgView);
        make.height.equalTo(@65);
    }];
    
    UILabel *shareLabel = [UILabel new];
    shareLabel.text = @"分享SHARE";
    shareLabel.textColor = ColorFrom16(0x292929);
    shareLabel.font = PFRFONT(18);
    shareLabel.textAlignment = 1;
    shareLabel.attributedText = [GW_Tool setTextSpace:shareLabel.text textSpaceSize:4.33];
    [_bottomView addSubview:shareLabel];
    [shareLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_bottomView.mas_centerY);
        make.left.equalTo(_bottomView).offset(20);
        make.width.greaterThanOrEqualTo(@82);
        make.height.equalTo(@25);
    }];
    
    UIButton *friendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [friendBtn setImage:IMAGE(@"share") forState:0];
    [_bottomView addSubview:friendBtn];
    [friendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_bottomView.mas_centerY);
        make.right.equalTo(_bottomView).offset(-20);
        make.width.equalTo(@43);
        make.height.equalTo(@43);
    }];

}

- (void)shareBtn:(UIButton *)sender
{
    _bgView.hidden = YES;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)handleTapAction
{
    [UIView animateWithDuration:1.0 animations:^{
        _bgView.hidden = YES;
        _bottomView.hidden = YES;
    }];
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if([touch.view isDescendantOfView:_aview] || [touch.view isDescendantOfView:_bottomView])
        return NO;
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
{
    _placeLabel.hidden = YES;
    return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if(textView.text.length == 0)
    {
        _placeLabel.hidden = NO;
    }
    
}

//上传图片
- (void)changeHeadImage
{
    if (_imageActionSheet) {
        _imageActionSheet = nil;
    }
    @weakobj(self);
    _imageActionSheet = [[ImageActionSheet alloc] initInVC:self isUserPortrait:YES];
    _imageActionSheet.resultBlock = ^(UIImage *image, NSString *imageBase64) {
        
        @strongobj(self);
        if (image) {
            [self uploadImage:image];
        }
    };
    [_imageActionSheet showInView:self.view];
}

//上传头像
- (void)uploadImage:(UIImage *)image {
    
    // image是原图片，imageBase64是base64编码
    // 下面调上传头像接口
    [SVProgressHUD show];
    @weakobj(self);
    NSArray *array = @[image];
    [NetWork uploadImageRequestURL:[NSString stringWithFormat:@"%@/image/Upload",GW_Domain] imageArray:array parameters:nil progress:^(float progress) {
        [SVProgressHUD showProgress:progress];
        
    } completionBlockWithSuccess:^(NSDictionary *resultDict) {
        
        @strongobj(self);
        MESSAGE_TOAST(@"上传成功");
        //        NSLog(@"%@", resultDict);
        NSArray *array = resultDict[@"data"];
        NSDictionary *dict = array[0];
        self.imageUrl = dict[@"uri"];
        [[NSUserDefaults standardUserDefaults] setObject:self.imageUrl forKey:@"imageurl"];
        if (self.imageUrl.length > 0) {
        }
    } andFailure:^(NSError *error, NSString *errorMsg) {
        NSLog(@"%@", error);
        MESSAGE_TOAST(errorMsg);
    }];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)ratingView:(LHRatingView *)view score:(CGFloat)score
{
    _score = score;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
